//function readCookie(name) {
//	var nameEQ = name + "=";
//	var ca = document.cookie.split(';');
//	for(var i=0;i < ca.length;i++) {
//		var c = ca[i];
//		while (c.charAt(0)==' ') c = c.substring(1,c.length);
//		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
//	}
//	return null;
//}
//
//var clang = readCookie('uxu123-lang');
//
//if(!clang)clang = 'en';

var appMetaData = {
        myapp: {
            base: './wf/intl/',
            modules : {
                "translator" : {
                    path: 'translator/translator.js',
                    lang: ["en","zh","zh-Hant", "ja", "ko"]
                }
            }
        }
    };

YUI(/*{debug: true,combine: false,filter:'raw'}*/
		{	
			lang:'en',
			groups:appMetaData,
	        modules: {
	            "node-menunav-core-css": {
	                type: "css",
	                path: "node-menunav/assets/node-menunav-core.css"
	            },
	            "custommenunavskin": {
	                type: "css",
	                fullpath: "/wf/settingmenu.css",
	                supersedes: ['skin-sam-node-menunav']
	            }
	        }
	    }
	).use('wfmenu','wfitemmenu','logindetector','wfdata','wfmodel','wfutils','projectliview','projectulview','projectpathview','wfhelppanel','wfaboutpanel','wfsharepanel','wfsync','wfwraper','warnbox','wffilter','event','event-custom','node', 'json', 'view','recordset-base','recordset-filter','recordset-indexer','recordset-sort','array-extras','yui-throttle','highlight','overlay','translator','wfintl', function (Y) {
	/*
	 * 找到父节点
	 */
	Y.projectListList = {},
	Y.enableLocal = "false",
	Y.lastTransId = 0,
	Y.loginUser = null,
	Y.lastSyncData = [],
	Y.processingCollide = false,
	Y.filterMode = false,
	Y.sharedMode = false,
	Y.readOnley = false,
	Y.ownerUserId = null;

	
	var history = new Y.HistoryHash();
	if(history.get('shared') && history.get('shared') == 'true' && history.get('item')){
		Y.sharedMode = true;
	}
	
	var filterInput = Y.one('.search-input');
	filterInput.set('value','');
    if (Y.config.win.localStorage) {
        Y.localStorage = Y.config.win.localStorage;
    }

	//其实是找到第一个UL的第一个LI，或者最后一个UL的最后一个LI，这个LiNode下面会有其他元素。比如table。
	//上面的描述有误，第一个UL可能还有子孙，也可能是关闭的情况，最后一个也是如此。
	
	var ProjectAppView;
	
// -- Project App View ------------------------------------------------------------
	ProjectAppView = Y.ProjectAppView = Y.Base.create('projectAppView', Y.View, [], {
	    // This is where we attach DOM events for the view. The `events` object is a
	    // mapping of selectors to an object containing one or more events to attach
	    // to the node(s) matching each selector.
	    events: {
	        '#addButton>span': {click: 'addTopProject'}
	    },
	    template: '',
	    initializer: function () {
	    	
	    },
	    render: function () {
	    	var ulv = new Y.AppView.ProjectUlView({parentId:Y.rootEntry});
	    	Y.one('#project-container').setContent(ulv.render().get('container'));
	        return this;
	    },
	    addTopProject:function(e){
	    	if(Y.readOnly){
	    		Y.showMsg('只读模式中',1000);
	    		return;
	    	}
	    	var list = Y.projectListList[Y.rootEntry];
	    	var position;
	    	if(list.size() > 0){
	    		position = list.item(list.size()-1).get('position') + 131072;
	    		if(position == 131072){
	    			position = position * list.size();
	    		}
	    	}else{
	    		position = 131072;
	    	}
	    	var dpItem = Y.totalDataHash[Y.rootEntry];
	    	var path = '';
	    	if(dpItem){
	    		path = dpItem.getValue('path') + ',' + dpItem.getValue('uuid');
	    		list.create({dp:Y.rootEntry,path:path,position:position,shareType:dpItem.getValue('shareType'),uid:dpItem.getValue('uid')});
	    	}else{
	    		var uid = 0;
	    		if(Y.loginUser){
	    			uid = Y.loginUser.id;
	    		}
	    		list.create({dp:Y.rootEntry,path:path,position:position,shareType:'none',uid:uid});
	    	}
	    	
	    }
	}, {
	    ATTRS: {
	        container: {
	        	setter:function(val){
	        		return Y.one(val);
	        	}
	        }
	    }
	});
		// -- Start your engines! ------------------------------------------------------
	Y.startApp = function(){
		new ProjectAppView({container:Y.one('#project-app')});
	};
	
	var projectAppView = new ProjectAppView({container:Y.one('#project-app')});
	
	var loginDetector = new Y.M3958.LoginDetector();
	loginDetector.startDetect();
	
    var firstLoadingPanel = new Y.Panel({
        srcNode: '#first-loading-panel',
        width  : 330,
        centered : Y.one('#hd'),
        zIndex : 101,
        modal  : true,
        render : true,
        buttons: []
    });
    
    var a = Y.WidgetPositionAlign;
    
    var smo = Y.one('#share-mode-overlay');
    
    if(Y.sharedMode){
        var shareModeOverLay = new Y.Overlay({
            srcNode:smo,
            width:"27em",
            height:"2.5em"
        });
        shareModeOverLay.render();
        shareModeOverLay.set("align", {/*node:'#project-app',*/points:[a.BC, a.BC]});
        shareModeOverLay.show();
        Y.on('scroll',function(e){
        	shareModeOverLay.set("align", {/*node:'#project-app',*/points:[a.BC, a.BC]});
        });
    }else{
    	Y.one('#share-mode-overlay').hide();
    }
    
    
    
    Y.all('.yui3-skin-sam .yui3-widget-mask').setStyle('backgroundColor','#FFFFFF');
    
    var firstRun = true;
    
	Y.on('dataReady',function(e){
		Y.projectListList = {};
		if(firstRun){
			firstLoadingPanel.show();
		}else{
			Y.showMsg(Y.translator.updatingView());
		}
		
		projectAppView.render();
		var q = history.get('q');
		if(q){
			Y.filterMode = true;
			filterInput.set('value',q);
			Y.all('.project-content').each(function(n){
				n.setContent(Y.Highlight.all(n.getContent(),q));
			});
			Y.all('.note-content').each(function(n){
				n.setContent(Y.Highlight.all(n.getContent(),q));
			});
		}else{
			Y.filterMode = false;
			filterInput.set('value','');
		}
		if(firstRun){
			firstLoadingPanel.get('boundingBox').transition({duration: 1,opacity: {
		        delay: 0,
		        duration: 1,
		        value: 0
		    }},function(){firstLoadingPanel.hide()});
		}else{
			Y.hideMsg();
		}
		firstRun = false;

		if(!Y.loginUser && !Y.sharedMode){
			Y.showMsg('您尚未登录系统!&nbsp;&nbsp;<a href="/notrpclogin?logout=true&next=workflow">登录</a>',3000);
		}
		var vp = new Y.AppView.ProjectPathView(); 
		vp.render();
		Y.startSync();
	});
});